﻿using System.Configuration;
using System.Linq;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4
{
    public class UserSettings
    {
        public string Locale { get; set; }

        private static UserSettings _settings;

        private UserSettings()
        {
            Locale = ConfigurationManager.AppSettings["Language"];
            if (string.IsNullOrEmpty(Locale))
            {
                Locale = LanguageManager.CurrentLanguage;
            }
        }

        public void Save()
        {
            Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            if (!cfg.AppSettings.Settings.AllKeys.Contains("Language"))
            {
                cfg.AppSettings.Settings.Add("Language", Locale);
            }
            else
            {
                cfg.AppSettings.Settings["Language"].Value = Locale;
            }

            cfg.Save(ConfigurationSaveMode.Modified);
        }

        public static UserSettings Load() => _settings ??= new UserSettings();
    }
}
